
package com.wyzz.hr.base.io;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.*;

import com.wyzz.hr.action.SalaryConfigAssist;
import com.wyzz.hr.base.Constants;
import com.wyzz.hr.bo.IEmpSalaryAcctitemsBo;
import com.wyzz.hr.pojo.Empsalaryacct;
import com.wyzz.hr.pojo.Empsalaryacctitems;
import com.wyzz.hr.pojo.Empsalaryacctversion;
import com.wyzz.hr.pojo.Empsalaryadj;
import com.wyzz.hr.pojo.Empsalaryconfig;
import com.wyzz.hr.pojo.Empsalarypay;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.MyTools;

public class Exportinfo implements Constants {

	public Exportinfo() {
	}

	public List[] getSalarySheetAndTitle(List decryResult,
			IEmpSalaryAcctitemsBo esaitemsBo) {
		Set nameSet = new HashSet();
		Set versionSet = new HashSet();
		Empsalaryacct acct = null;
		Empsalaryacctversion version = null;
		for (int i = 0; i < decryResult.size(); i++) {
			acct = ((Empsalarypay) decryResult.get(i)).getEspEsavId()
					.getEsavEsac();
			if (nameSet.add(acct.getEsacName())) {
				version = ((Empsalarypay) decryResult.get(i)).getEspEsavId();
				versionSet.add(version);
			}
		}

		List titlesList[] = new List[versionSet.size()];
		Iterator versionIterator = versionSet.iterator();
		for (int index = 0; versionIterator.hasNext(); index++) {
			List stringList = new ArrayList();
			version = (Empsalaryacctversion) versionIterator.next();
			stringList.add(version.getEsavEsac().getEsacName());
			stringList = getTitle(version.getId(), esaitemsBo, stringList);
			titlesList[index] = stringList;
		}

		return titlesList;
	}

	public List[] getPayslipTitle(List decryResult,
			IEmpSalaryAcctitemsBo esaitemsBo) {
		int assistListSize = decryResult.size();
		Set bigTitleNameSet = new HashSet();
		List payrollTitleList = new ArrayList();
		String deptName = null;
		Empsalaryacct acct = null;
		Empsalaryacctversion version = null;
		for (int i = 0; i < assistListSize; i++) {
			deptName = ((Empsalarypay) decryResult.get(i)).getEspDept()
					.getDepartmentName();
			acct = ((Empsalarypay) decryResult.get(i)).getEspEsavId()
					.getEsavEsac();
			version = ((Empsalarypay) decryResult.get(i)).getEspEsavId();
			String bigTitleName = (new StringBuilder()).append(deptName)
					.append(",").append(acct.getEsacName()).append(",").append(
							version.getId()).toString();
			if (bigTitleNameSet.add(bigTitleName))
				payrollTitleList.add(bigTitleName);
		}

		Collections.sort(payrollTitleList);
		List titlesList[] = new List[payrollTitleList.size()];
		for (int i = 0; i < payrollTitleList.size(); i++) {
			List stringList = new ArrayList();
			String array[] = ((String) payrollTitleList.get(i)).split(",");
			stringList.add(array[0]);
			stringList.add(array[1]);
			stringList = getPayslipTitle(array[2], esaitemsBo, stringList);
			titlesList[i] = stringList;
		}

		return titlesList;
	}

	public List[] getSalaryadjSheetAndTitle(List result,
			IEmpSalaryAcctitemsBo esaitemsBo) {
		int adjListSize = result.size();
		Set stringSet = new HashSet();
		Set empsalaryadjSet = new HashSet();
		for (int i = 0; i < adjListSize; i++) {
			Empsalaryadj empsalaryAdj = (Empsalaryadj) result.get(i);
			String sheetName = (new StringBuilder()).append(
					empsalaryAdj.getEsaEsavIdCur().getEsavEsac().getEsacName())
					.append("-->").append(
							empsalaryAdj.getEsaEsavIdPro().getEsavEsac()
									.getEsacName()).toString();
			if (stringSet.add(sheetName))
				empsalaryadjSet.add(empsalaryAdj);
		}

		int totalSize = empsalaryadjSet.size();
		List titlesList[] = new List[totalSize];
		Iterator adjIterator = empsalaryadjSet.iterator();
		for (int index = 0; adjIterator.hasNext(); index++) {
			List stringList = new ArrayList();
			Empsalaryadj empsalaryAdj = (Empsalaryadj) adjIterator.next();
			String sheetName = (new StringBuilder()).append(
					empsalaryAdj.getEsaEsavIdCur().getEsavEsac().getEsacName())
					.append("-->").append(
							empsalaryAdj.getEsaEsavIdPro().getEsavEsac()
									.getEsacName()).toString();
			stringList.add(sheetName);
			stringList = getEmpsalaryadjTitle(empsalaryAdj, stringList);
			titlesList[index] = stringList;
		}

		return titlesList;
	}

	public List[] getSheetAndTitle(List salaryConfigAssistList,
			IEmpSalaryAcctitemsBo esaitemsBo) {
		int assistListSize = salaryConfigAssistList.size();
		Set nameSet = new HashSet();
		Set assistSet = new HashSet();
		for (int i = 0; i < assistListSize; i++) {
			SalaryConfigAssist assist = (SalaryConfigAssist) salaryConfigAssistList
					.get(i);
			if (nameSet.add(assist.getAcceptName()))
				assistSet.add(assist);
		}

		int totalSize = assistSet.size();
		List titlesList[] = new List[totalSize];
		Iterator acctIterator = assistSet.iterator();
		for (int index = 0; acctIterator.hasNext(); index++) {
			List stringList = new ArrayList();
			SalaryConfigAssist assist = (SalaryConfigAssist) acctIterator
					.next();
			stringList.add(assist.getAcceptName());
			stringList = getTitle(assist.getSalaryAccept(), esaitemsBo,
					stringList);
			titlesList[index] = stringList;
		}

		return titlesList;
	}

	public List[] getSalaryResult(List decryResult, List titles[]) {
		int size = titles.length;
		String acceptNameArray[] = new String[size];
		List result[] = new ArrayList[size];
		for (int i = 0; i < size; i++) {
			acceptNameArray[i] = (String) titles[i].get(0);
			result[i] = new ArrayList();
		}

		label0: for (int i = 0; i < decryResult.size(); i++) {
			Empsalarypay empsalarypay = (Empsalarypay) decryResult.get(i);
			int j = 0;
			do {
				if (j >= size)
					continue label0;
				if (empsalarypay.getEspEsavId().getEsavEsac().getEsacName() == acceptNameArray[j]) {
					result[j].add(empsalarypay);
					continue label0;
				}
				j++;
			} while (true);
		}

		return result;
	}

	public List[] getPayslipResult(List decryResult, List titles[]) {
		int size = titles.length;
		String deptNameArray[] = new String[size];
		String acceptNameArray[] = new String[size];
		List result[] = new ArrayList[size];
		for (int i = 0; i < size; i++) {
			deptNameArray[i] = (String) titles[i].get(0);
			acceptNameArray[i] = (String) titles[i].get(1);
			result[i] = new ArrayList();
		}

		label0: for (int i = 0; i < decryResult.size(); i++) {
			Empsalarypay empsalarypay = (Empsalarypay) decryResult.get(i);
			int j = 0;
			do {
				if (j >= size)
					continue label0;
				if (empsalarypay.getEspDept().getDepartmentName().equals(
						deptNameArray[j])
						&& empsalarypay.getEspEsavId().getEsavEsac()
								.getEsacName().equals(acceptNameArray[j])) {
					result[j].add(empsalarypay);
					continue label0;
				}
				j++;
			} while (true);
		}

		return result;
	}

	public List[] getSalaryadjResult(List exportResult, List titles[]) {
		int size = titles.length;
		String salaryadjName[] = new String[size];
		List result[] = new ArrayList[size];
		for (int i = 0; i < size; i++) {
			salaryadjName[i] = (String) titles[i].get(0);
			result[i] = new ArrayList();
		}

		label0: for (int i = 0; i < exportResult.size(); i++) {
			Empsalaryadj empsalaryAdj = (Empsalaryadj) exportResult.get(i);
			String sheetName = (new StringBuilder()).append(
					empsalaryAdj.getEsaEsavIdCur().getEsavEsac().getEsacName())
					.append("-->").append(
							empsalaryAdj.getEsaEsavIdPro().getEsavEsac()
									.getEsacName()).toString();
			int j = 0;
			do {
				if (j >= size)
					continue label0;
				if (sheetName.equals(salaryadjName[j])) {
					result[j].add(empsalaryAdj);
					continue label0;
				}
				j++;
			} while (true);
		}

		return result;
	}

	public List[] getConfigResult(List salaryConfigAssistList, List titles[]) {
		int size = titles.length;
		String acceptNameArray[] = new String[size];
		List result[] = new ArrayList[size];
		for (int i = 0; i < size; i++) {
			acceptNameArray[i] = (String) titles[i].get(0);
			result[i] = new ArrayList();
		}

		label0: for (int i = 0; i < salaryConfigAssistList.size(); i++) {
			SalaryConfigAssist salaryConfigAssist = (SalaryConfigAssist) salaryConfigAssistList
					.get(i);
			int j = 0;
			do {
				if (j >= size)
					continue label0;
				if (salaryConfigAssist.getAcceptName() == acceptNameArray[j]) {
					result[j].add(salaryConfigAssist);
					continue label0;
				}
				j++;
			} while (true);
		}

		return result;
	}

	private List getSalaryConfigList(Empsalaryconfig config, String key)
			throws Exception {
		List list = new ArrayList();
		Class ownerClass = config.getClass();
		for (int i = 1; i <= 48; i++) {
			Method confMethod = ownerClass.getMethod((new StringBuilder())
					.append("getEscColumn").append(i).toString(), new Class[0]);
			list.add(MyTools.vigenere((BigDecimal) confMethod.invoke(config,
					new Object[0]), key, MyTools.DECRYPT_MODE));
		}

		return list;
	}

	public String getEmpStaus(Integer i) {
		switch (i.intValue()) {
		case 0: // '\0'
			return "\u79BB\u804C";

		case 1: // '\001'
			return "\u5728\u804C";
		}
		return null;
	}

	private List getTitle(String versionId, IEmpSalaryAcctitemsBo esaitemsBo,
			List stringList) {
		stringList.add("\u5DE5\u53F7");
		stringList.add("\u59D3\u540D");
		stringList.add("\u90E8\u95E8");
		stringList.add("\u804C\u4F4D");
		stringList.add("\u7528\u5DE5\u5F62\u5F0F");
		stringList.add("\u5DE5\u4F5C\u5730\u533A");
		stringList.add("\u5458\u5DE5\u72B6\u6001");
		stringList.add("\u94F6\u884C\u5E10\u53F7");
		stringList.add("\u94F6\u884C\u5F00\u6237\u884C");
		stringList.add("\u6210\u672C\u4E2D\u5FC3");
		stringList.add("\u85AA\u8D44\u7EA7\u522B");
		stringList.add("\u85AA\u8D44\u5E10\u5957");
		List itemsList = esaitemsBo.getItemsByAcctversion(versionId);
		int size = itemsList.size();
		for (int i = 0; i < size; i++) {
			String string = (new StringBuilder()).append("A").append(i + 1)
					.append("-").append(
							((Empsalaryacctitems) itemsList.get(i))
									.getEsaiEsdd().getEsddName()).toString();
			stringList.add(string);
		}

		return stringList;
	}

	private List getPayslipTitle(String versionId,
			IEmpSalaryAcctitemsBo esaitemsBo, List stringList) {
		stringList.add("\u5DE5\u53F7");
		stringList.add("\u59D3\u540D");
		stringList.add("\u7528\u5DE5\u5F62\u5F0F");
		stringList.add("\u7ECF\u7406\u59D3\u540D");
		stringList.add("\u72B6\u6001");
		stringList.add("\u94F6\u884C\u5E10\u53F7");
		stringList.add("\u85AA\u8D44\u7EA7\u522B");
		List itemsList = esaitemsBo.getItemsByAcctversion(versionId);
		int size = itemsList.size();
		for (int i = 0; i < size; i++) {
			String string = ((Empsalaryacctitems) itemsList.get(i))
					.getEsaiEsdd().getEsddName();
			stringList.add(string);
		}

		return stringList;
	}

	private List getEmpsalaryadjTitle(Empsalaryadj empSalaryadj, List stringList) {
		stringList.add("\u5DE5\u53F7");
		stringList.add("\u59D3\u540D");
		stringList.add("\u90E8\u95E8");
		stringList.add("\u7528\u5DE5\u5F62\u5F0F");
		stringList.add("\u5DE5\u4F5C\u5730\u533A");
		stringList.add("\u52A0\u85AA\u751F\u6548\u65E5\u671F");
		stringList.add("\u672C\u6B21\u52A0\u85AA\u5E45\u5EA6");
		stringList.add("\u52A0\u85AA\u5907\u6CE8");
		stringList.add("\u52A0\u85AA\u72B6\u6001");
		stringList.add("\u8C03\u85AA\u524D\u85AA\u8D44\u7EA7\u522B");
		stringList.add("\u8C03\u85AA\u540E\u85AA\u8D44\u7EA7\u522B");
		stringList.add("\u8C03\u85AA\u524D\u85AA\u8D44\u5E10\u5957");
		stringList.add("\u8C03\u85AA\u540E\u85AA\u8D44\u5E10\u5957");
		IEmpSalaryAcctitemsBo esaitemsBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
				.getBean("empsalaryacctitemsBo");
		List itemsCur = esaitemsBo.getItemsByAcctversion(empSalaryadj
				.getEsaEsavIdCur().getId());
		int sizeCur = itemsCur.size();
		for (int i = 0; i < sizeCur; i++) {
			int index = i + 1;
			String titleName = (new StringBuilder()).append("A").append(index)
					.append("-").append(
							((Empsalaryacctitems) itemsCur.get(i))
									.getEsaiEsdd().getEsddName()).toString();
			stringList.add(titleName);
		}

		for (int max = sizeCur; max < 48; max++) {
			int index = max + 1;
			String titleName = (new StringBuilder()).append("A").append(index)
					.toString();
			stringList.add(titleName);
		}

		List itemsPro = esaitemsBo.getItemsByAcctversion(empSalaryadj
				.getEsaEsavIdPro().getId());
		int sizePro = itemsPro.size();
		for (int i = 0; i < sizePro; i++) {
			int index = i + 1;
			String titleName = (new StringBuilder()).append("B").append(index)
					.append("-").append(
							((Empsalaryacctitems) itemsPro.get(i))
									.getEsaiEsdd().getEsddName()).toString();
			stringList.add(titleName);
		}

		for (int max = sizePro; max < 48; max++) {
			int index = max + 1;
			String titleName = (new StringBuilder()).append("B").append(index)
					.toString();
			stringList.add(titleName);
		}

		return stringList;
	}
}
